// 添加sequelize实例的导入
const { sequelize } = require('../config/db');
const { DataTypes } = require('sequelize');

// 定义诊所模型
const Clinic = sequelize.define('Clinic', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  name: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '诊所名称'
  },
  address: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '诊所地址'
  },
  distance: {
    type: DataTypes.FLOAT,
    allowNull: false,
    comment: '距离'
  },
  availableCount: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    comment: '可预约数量'
  },
  vaccineTypeIds: {
    type: DataTypes.STRING,
    allowNull: false,
    get() {
      const value = this.getDataValue('vaccineTypeIds');
      return value ? JSON.parse(value) : [];
    },
    set(value) {
      this.setDataValue('vaccineTypeIds', JSON.stringify(value));
    },
    comment: '支持的疫苗类型ID列表'
  }
}, {
  tableName: 'clinics',
  comment: '诊所表',
  timestamps: true, // 自动添加 createdAt 和 updatedAt 字段
  indexes: [
    {
      fields: ['name'],
      unique: true
    }
  ]
});

module.exports = { Clinic };